<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="积累现金奖励" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入积累现金奖励"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['marketing:awardpage:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['marketing:awardpage:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['marketing:awardpage:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="awardpageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="营销活动id" align="center" prop="id"/>
      <el-table-column label="名称" align="center" prop="marketingActivity.activityName"/>
      <el-table-column label="创建时间" align="center" prop="marketingActivity.createsTime"/>
      <el-table-column label="有效时间" align="center" prop="marketingActivity.endTime"/>
      <el-table-column label="参与班级数(/个)" align="center" prop="grades"/>
      <el-table-column label="参加人数(/人)" align="center" prop="person"/>
      <el-table-column label="积累现金奖励(/元)" align="center" prop="money"/>
      <el-table-column label="状态" align="center" prop="marketingActivity.state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.m_start" :value="scope.row.marketingActivity.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail"
          >查看明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营销活动列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动id" prop="activityId">
          <el-input v-model="form.activityId" placeholder="请输入活动id"/>
        </el-form-item>
        <el-form-item label="参与班级数" prop="grades">
          <el-input v-model="form.grades" placeholder="请输入参与班级数"/>
        </el-form-item>
        <el-form-item label="参加人数" prop="person">
          <el-input v-model="form.person" placeholder="请输入参加人数"/>
        </el-form-item>
        <el-form-item label="积累现金奖励" prop="money">
          <el-input v-model="form.money" placeholder="请输入积累现金奖励"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看明细 参加人数 -->
    <el-dialog :title="title" :visible.sync="open_person" width="1200px">
      <el-table v-loading="loading" :data="awardPersonList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="姓名" align="center" prop="user.nickName"/>
        <el-table-column label="手机号" align="center" prop="user.phonenumber"/>
        <el-table-column label="剩余抽奖次数" align="center" prop="prizeNum"/>
        <el-table-column label="中奖次数" align="center" prop="drawNum"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDetail2"
            >查看明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 查看明细 查看奖励 -->
    <el-dialog :title="title" :visible.sync="open_person2" width="1200px">
      <el-table v-loading="loading" :data="awardDetailList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="任务" align="center" prop="task.taskName"/>
        <el-table-column label="时间" align="center" prop="awardTime"/>
        <el-table-column label="奖励" align="center" prop="marketingAward.awardName"/>
        <el-table-column label="领取方式" align="center" prop="pickup">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.m_pickup_method" :value="scope.row.pickup"/>
          </template>
        </el-table-column>
        <el-table-column label="领取状态" align="center" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.m_pickup_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAwardpage,
  getAwardpage,
  delAwardpage,
  addAwardpage,
  updateAwardpage,
  listAwardPerson,
  listAwardDetail
} from '@/api/marketing/awardpage'

export default {
  name: 'Awardpage',
  dicts: ['m_pickup_state', 'm_pickup_method','m_start'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销活动列表表格数据
      awardpageList: [],
      // 查看明细 - 查询参加活动人数
      awardPersonList: [],
      // 查看明细 - 查询参加活动奖励
      awardDetailList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      open_person: false,
      open_person2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        money: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getPersonList()
    this.getDetailList()
  },
  methods: {
    /** 查询营销活动列表列表 */
    getList() {
      this.loading = true
      listAwardpage(this.queryParams).then(response => {
        this.awardpageList = response.rows
        console.log('this.awardpageList', this.awardpageList)
        this.total = response.total
        this.loading = false
      })
    },
    /** 查看明细 - 查询参加活动人数 */
    getPersonList() {
      this.loading = true
      listAwardPerson().then(res => {
        this.awardPersonList = res.rows
        console.log('查看明细', this.awardPersonList)
        this.loading = false
      })
    },
    /** 查看明细 - 查询参加活动奖励 */
    getDetailList(){
      this.loading = true
      listAwardDetail().then(res => {
        this.awardDetailList = res.rows
        console.log('查看明细奖励', this.awardDetailList)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        activityId: null,
        grades: null,
        person: null,
        money: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加营销活动列表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAwardpage(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改营销活动列表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAwardpage(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAwardpage(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除营销活动列表编号为"' + ids + '"的数据项？').then(function() {
        return delAwardpage(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    handleDetail() {
      this.open_person = true
    },
    handleDetail2() {
      this.open_person = false
      this.open_person2 = true
    }
  }
}
</script>
